#include <iostream>
#include <deque>

using namespace std;

const int N = 1e6 + 10;
int a[N];
int ret1[N], ret2[N];
int n, k;

int main()
{
	cin >> n >> k;
	for(int i = 1; i <= n; i++) cin >> a[i];
	deque<int> q;
	for(int i = 1; i <= n; i++)
	{
		while(q.size() && a[i] <= a[q.back()]) q.pop_back();
		q.push_back(i);
		if(q.back() - q.front() + 1 > k) q.pop_front();
		if(i >= k) ret2[i] = q.front(); 
	}
	for(int i = k; i <= n; i++)
	{
		cout << a[ret2[i]] << " ";
	}
	cout << endl;
	while(q.size()) q.pop_back();
	for(int i = 1; i <= n; i++)
	{
		while(q.size() && a[i] >= a[q.back()]) q.pop_back();
		q.push_back(i);
		if(q.back() - q.front() + 1 > k) q.pop_front();
		if(i >= k) ret1[i] = q.front(); 
	}
	for(int i = k; i <= n; i++)
	{
		cout << a[ret1[i]] << " ";
	}
	return 0;
}
